<?php

namespace App\Models\RBAC;

use Illuminate\Database\Eloquent\SoftDeletes;
use Spatie\Permission\Models\Permission as BasePermission;

/**
 * Class Permission.
 *
 * @package namespace App\Models;
 */
class Permission extends BasePermission
{

    use SoftDeletes;
    const TABLE_NAME = '';

    protected $fillable = [
        'root_id',
        'pid',
        'icon',
        'guard_name',
        'name',
        'route',
        'display_name',
        'is_menu',
        'position',
        'params',
        'description'
    ];

    protected $appends = ['self_name'];


    /****************** Relationship *********************/

    /**
     * 子权限项
     * User: Zhangxy
     * Date: 2016-05-29
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     */
    public function subPermissions()
    {
        return $this->hasMany('App\Models\RBAC\Permission', 'pid')->orderBy('position', "desc")->orderBy('id', "asc");
    }

    /**
     * 父权限项
     * User: Zhangxy
     * Date: 2018-08-13
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     */
    public function parentPermission()
    {
        return $this->belongsTo('App\Models\RBAC\Permission', 'pid')->orderBy('id', "asc");
    }

    /****************** Attribute *********************/

    /**
     * User: Zhangxy
     * Date: 2018-08-13
     * @return mixed
     */
    public function getSelfNameAttribute()
    {
        $index = strrchr($this->name, '.');
        return empty($index) ? $this->name : substr($index, 1);
    }

    /****************** Scope *********************/

    /**
     * 菜单
     * @param $query
     * User: Zhangxy
     * Date: 2018-08-13
     * @return mixed
     */
    public function scopeIsMenu($query)
    {
        return $query->where("is_menu", 1);
    }

    /**
     * 根菜单
     * @param $query
     * User: Zhangxy
     * Date: 2018-08-13
     * @return mixed
     */
    public function scopeIsRoot($query)
    {
        return $query->where("pid", 0);
    }

    /****************** Function *********************/


    /**
     * 显示 菜单
     * User: Zhangxy
     * Date: 2018-08-13
     * @return string
     */
    public function displayIsMenu()
    {
        return $this->is_menu ? "是" : "否";
    }


    /**
     * 获取显示的uri
     * @param string $uriPrefix
     * @param string $params
     * User: Zhangxy
     * Date: 2018-08-13
     * @return string
     */
    public function getDisplayUri($uriPrefix = '', $params = '')
    {
        $uri = 'javascript:;';
        try {
            if (empty($uriPrefix)) {
                if (!empty($this->route)) {
                    $uriPrefix = $this->route;
                } else {
                    $uriPrefix = $this->name;
                }
            }

            // echo $uriPrefix;
            $uri = route($uriPrefix);
            if (empty($params)) {
                $params = $this->params;
            }
            if (!empty($params)) {
                $rParams = json_decode($params, true);
                $uriPPP = "";
                if ($rParams) {
                    foreach ($rParams as $pk => $pv) {
                        $uriPPP .= sprintf("&%s=%s", $pk, $pv);
                    }
                    $uri .= "?" . $uriPPP;
                }
            } else {
                $uri = route($uriPrefix);
            }
        } catch (\Exception $e) {
//            mm($uriPrefix);
        }
        return $uri;
    }

}
